#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. "/minioslib" || exit 1

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# install packages
install_packages

sed -i 's/^#EXTRA_GROUPS=.*/EXTRA_GROUPS="adm cdrom sudo dip plugdev users lpadmin sambashare audio docker libvirt wireshark"/' /etc/adduser.conf
sed -i 's/^#ADD_EXTRA_GROUPS=.*/ADD_EXTRA_GROUPS=1/' /etc/adduser.conf

if [ -d /etc/NetworkManager ]; then
    if [ ! -d /etc/NetworkManager/dispatcher.d ]; then
        mkdir -p /etc/NetworkManager/dispatcher.d
    fi
fi
if [ -d /etc/NetworkManager/dispatcher.d ]; then
    cat <<'EOF' >/etc/NetworkManager/dispatcher.d/09-timedate
#!/bin/sh
case "$2" in
    up)
        sleep 3
        # Check the availability of https://ipapi.co/timezone over a minute at intervals
        for i in {1..60}
        do
            if curl --output /dev/null --silent --head --fail https://ipapi.co/timezone
            then
                timedatectl set-local-rtc 0
                timedatectl set-ntp true
                timedatectl set-timezone "$(curl --fail https://ipapi.co/timezone)"
                break
            else
                echo "https://ipapi.co/timezone is not available. Attempt number $i..."
                sleep 1
            fi
        done
    ;;
esac
EOF
    chmod +x /etc/NetworkManager/dispatcher.d/09-timedate
fi
